package com.hammer.m3u8downloader.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hammer.m3u8downloader.R;
import com.hammer.m3u8downloader.entity.M3U8DownloadItemEntity;

import java.util.List;

public class M3U8DownloadListAdapter extends ArrayAdapter<M3U8DownloadItemEntity> {
    private int mResourceId;
    private LayoutInflater inflater;
    private ListView listView;
    private M3U8ManagerListAdapter.OnItemClickListener onItemClickListener;

    public M3U8DownloadListAdapter(Context context, int resource, ListView listView, List<M3U8DownloadItemEntity> objects) {
        super(context, resource, objects);
        this.mResourceId = resource;
        this.inflater = LayoutInflater.from(context);
        this.listView = listView;
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
        final M3U8DownloadItemEntity item = getItem(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(mResourceId, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (item != null) {
            viewHolder.title.setText(item.getTaskName());
            viewHolder.speed.setText("0 K/s");
            if (item.getStatus() == 4) {
                viewHolder.info.setText("任务排队，等待下载");
                viewHolder.progress.setIndeterminate(true);
            } else if (item.getStatus() == 6) {
                viewHolder.info.setText("准备下载请求中...");
                viewHolder.progress.setIndeterminate(true);
            } else {
                viewHolder.info.setText("任务暂停");
                viewHolder.progress.setIndeterminate(false);
            }

            viewHolder.progress.setProgress(item.getProgress());
        }
        if (onItemClickListener != null) {
            convertView.setOnClickListener(view -> onItemClickListener.onItemClick(position, item));
        }
        return convertView;
    }

    public class ViewHolder {
        public TextView title;
        public TextView speed;
        public ProgressBar progress;
        public TextView info;

        private ViewHolder(View viewRoot) {
            title = viewRoot.findViewById(R.id.tv_title);
            speed = viewRoot.findViewById(R.id.tv_speed);
            progress = viewRoot.findViewById(R.id.progress);
            info = viewRoot.findViewById(R.id.tv_info);
        }
    }

    private View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
        if (pos < firstListItemPosition || pos > lastListItemPosition) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }

    public M3U8DownloadListAdapter.ViewHolder getViewHolder(int position) {
        return (M3U8DownloadListAdapter.ViewHolder) getViewByPosition(position, listView).getTag();
    }

    public boolean isShowing(int position) {
        int showViewCount = listView.getChildCount();
        int lastPosition = listView.getLastVisiblePosition();
        return position <= lastPosition && position > lastPosition - showViewCount;
    }


    public void setOnItemClickListener(M3U8ManagerListAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, M3U8DownloadItemEntity itemEntity);
    }
}
